package com.china.fragment;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.china.adapter.TweetAdapter;
import com.china.base.BaseFragment;
import com.china.osourcechina.R;
import com.china.osourcechina.TweetDetailActivity;
import com.china.utils.AddressUtils;
import com.china.utils.HttpUtils;
import com.china.vo.Move;
import com.china.vo.Tweet;
import com.thoughtworks.xstream.XStream;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2016/5/9.
 */
public class MoveFailsfragment extends BaseFragment {

    private int uid = 0;
    private SwipeRefreshLayout swipe_container;
    private ListView fg_tweet_list;
    private List<Tweet> tweetList;
    private String string = null;

    @Override
    protected int getLoadViewId() {
        uid = getArguments().getInt("uid");
        if(uid==1){
            return R.layout.fragment_minemove;
        }
        return R.layout.fragment_newestmove;
    }

    @Override
    protected void initView(View view) {

        if(uid!=1) {
            swipe_container = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
            fg_tweet_list = (ListView) view.findViewById(R.id.fg_tweet_list);
            //设置刷新时动画的颜色，可以设置4个
            swipe_container.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    string = HttpUtils.post(AddressUtils.blog_move + "?uid=" + uid + "&pageindex=" + 0 + "&pagesize=3");
                    XStream x = new XStream();
                    x.processAnnotations(Move.class);
                    Move os = new Move();
                    os = (Move) x.fromXML(string);
                    Message msg = new Message();
                    msg.obj = os.getTweets().getTweet();
                    handler.sendMessage(msg);
                }
            }).start();
            swipe_container.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

                @Override
                public void onRefresh() {
                    // TODO Auto-generated method stub
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            swipe_container.setRefreshing(false);
                        }
                    }, 2000);
                }
            });
        }
    }

    @Override
    protected void initData() {

        if(uid!=1) {
            fg_tweet_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getActivity(), TweetDetailActivity.class);
                    intent.putExtra("url", tweetList.get(position).getPortrait());
                    intent.putExtra("uname", tweetList.get(position).getAuthor());
                    intent.putExtra("body", tweetList.get(position).getBody());
                    intent.putExtra("time",tweetList.get(position).getPubDate());
                    intent.putExtra("count",tweetList.get(position).getCommentCount());
                    intent.putExtra("list", (Serializable) tweetList.get(position).getLikeList().getUser());
                    startActivity(intent);
                }
            });
        }
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            tweetList = (List<Tweet>) msg.obj;
            TweetAdapter adapter = new TweetAdapter(tweetList,getActivity());
            fg_tweet_list.setAdapter(adapter);
        }
    };

}
